package com.tgy.algorithm.base;

public class PrintNumBit {
    public static String printNumBit(long num, int bit) {
        StringBuilder builder = new StringBuilder();
        for (int i = bit - 1; i >= 0; i--) {
            long val = (num >> i) & 1;
            builder.append(val);
            if (i % 4 == 0) {
                builder.append(" ");
            }
        }
        String result = builder.toString();
        System.out.println(result);
        return result;
    }

    public  static  void printNumBit2(int num) {
        for (int i = 31; i >= 0; i--) {
            System.out.print((num & (1 << i)) == 0 ? "0" : "1");
        }
        System.out.println();
    }

    public static void test01(){
        // 8 1000 2^3
        // 负数个数是 2^31
        System.out.println("负数的范围");
        printNumBit(Integer.MIN_VALUE,32);
        printNumBit(-1,32);

        System.out.println("中间的0");
        printNumBit(0,32);
        // 正数个数 2^31 - 1
        System.out.println("正数的范围");
        printNumBit(1,32);
        printNumBit(Integer.MAX_VALUE,32);
    }

    public static void test02(){
        int a = 313123;
        int b = ~a;
        printNumBit(a,Integer.SIZE);
        printNumBit(b,Integer.SIZE);
    }

    public static void test03(){
        int one = -10;
        printNumBit(one,Integer.SIZE);
        // 左侧无符合填充
        int two = one >>> 1;
        printNumBit(two,Integer.SIZE);
        // 左侧有符合填充
        int three = one >> 1;
        printNumBit(three,Integer.SIZE);
    }

    public static void test04() {
        // 取相反数
        int one = 10;
        printNumBit(one,Integer.SIZE);
        printNumBit(~one,Integer.SIZE);
        int two = ~one + 1;
        System.out.println(two);
        // 最小值取反加1还是本身，0也是
        printNumBit(Integer.MIN_VALUE,Integer.SIZE);
    }

    public static void main(String[] args) {
        test04();
    }

}